<?php
/**
 * Created by PhpStorm.
 * User: 钊鑫
 * Date: 2019/1/25
 * Time: 13:58
 */
namespace app\api\service;

use think\facade\Cache;
use app\api\model\Homeuser;
use app\api\service\WxToken as WxTokenService;
use think\Db;

class WxLogin
{
    static function register($post){
        $sign =   Cache::get($post['sign']);//获取短信标识
        if(!$sign) {
            $data = array('status' => -1, 'msg' => '验证码已失效，请重新获取！');
        }else if($sign['number'] != $post['number']) {
            $data = array('status' => -1, 'msg' => '手机号已更改，请重新获取！');
        }else if($sign['code'] != $post['smscode']) {
            $data = array('status' => -1, 'msg' => '验证码输入错误，请重新输入！');
        }else if($sign['code'] == $post['smscode']) {
            if($post['sex'] == '1' && $post['type'] == '1')//男老师
                $post['image'] = 'share/images/teacher_0.jpg';
            else if($post['sex'] == '2' && $post['type'] == '1')//女老师
                $post['image'] = 'share/images/teacher_1.jpg';
            else if($post['sex'] == '2' && $post['type'] == '2')//女学生
                $post['image'] = 'share/images/student_1.jpg';
            else if($post['sex'] == '1' && $post['type'] == '2')//男学生
                $post['image'] = 'share/images/student_0.jpg';
            if(isset($post['student_id'])) {
                $post['params']['student_id'] = $post['student_id'];
            }
//            // 启动事务
//            Db::startTrans();
//            try {
                $Model = new Homeuser();
                $result = $Model->save($post);
                if($result){
                    $usertoken = new WxTokenService($post['xcxcode'], $Model->id);
                    $usertoken = $usertoken->getToken();
                    if($usertoken) {
                        $data = array('status' => 1, 'msg' =>$usertoken);
                    }
                }
                // 提交事务
//                Db::commit();
//            } catch (\Exception $e) {
//                $data = array('status' => -1, 'msg' => '请先获取微信code！');
//                // 回滚事务
//                Db::rollback();
//            }
        }else {
            $data = array('status' => -1, 'msg' => '其他未知错误，请重新注册！');
        }

        return $data;
    }
}
